import 'dart:ffi';

import 'package:animal/constant/color.dart';
import 'package:animal/constant/font.dart';
import 'package:animal/utils/screen_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

//界面状态
class PageStateWidget {
  static Widget stateWidget({
    Widget? contentWidget,
    PageState pageStatue = PageState.none,
    String? emptyString = "暂时还没有数据噢",
    String? emptyImage,
    String? errorString,
    Function()? refreshTap, //重新加载点击事件
    bool? hideImg = false,
  }) {
    if (errorString == "" || errorString == null) {
      errorString = "网络连接故障啦";
    }
    if (pageStatue == PageState.loading) {
      return loadingWidget(hideImg: hideImg!);
    }
    if (pageStatue == PageState.error) {
      return errorWidget(errorString, refreshTap!, hideImg: hideImg!);
    }
    if (pageStatue == PageState.empty) {
      return emptyWidget(emptyString!, refreshTap!, hideImg: hideImg!,emptyImage: emptyImage);
    }
    return contentWidget!;
  }

  ///加载中展示的布局
  static Widget loadingWidget({bool hideImg = false}) {
    return Center(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Visibility(
          visible: !hideImg,
          child: Container(
            margin: EdgeInsets.only(bottom: ScreenUtil().L(60)),
            width: ScreenUtil().L(150),
            height: ScreenUtil().L(150),
            padding: EdgeInsets.all(ScreenUtil().l(20)),
            child: const CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(KColorConstant.theme),
            ),
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("数据加载中...", style: KFontConstant.myTextStyle(color: Colors.black87, size: 17)),
          ],
        )
      ],
    ));
  }

  ///数据为空时展示的布局
  static Widget emptyWidget(String? noDataString, Function()? refreshTap, {bool? hideImg = false,String? emptyImage}) {
    return Center(
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Visibility(
          visible: !hideImg!,
          child: Container(
            margin: EdgeInsets.only(bottom: ScreenUtil().L(60)),
            child: Image.asset(
              emptyImage??"icon/empty.png",
              width: ScreenUtil().L(150),
            ),
          ),
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(noDataString!, style: KFontConstant.myTextStyle(color: Colors.black87, size: 17)),
            Container(height: ScreenUtil().L(15),),
            CupertinoButton(
              onPressed:refreshTap!,
              color: const Color(0X00000000),
              child: Text(
                "刷新重试",
                style: KFontConstant.myTextStyle(size: 17, color: KColorConstant.btnColor19ACE5),
              ),
            ),
            Container(
                margin: EdgeInsets.only(left: ScreenUtil().L(20)),
                child:
                    InkWell(onTap: () => refreshTap, child: Text("", style: KFontConstant.myTextStyle(color: const Color(0xff0059B9), size: 17, bold: true)))),
          ],
        )
      ],
    ));
  }

  ///出错时展示的布局
  static Widget errorWidget(String? errorString, Function()? refreshTap, {bool? hideImg = false}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: ScreenUtil().L(20)),
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Visibility(
              visible: !hideImg!,
              child: Container(
                margin: EdgeInsets.only(bottom: ScreenUtil().L(60)),
                child: Image.asset(
                  "icon/error.png",
                  width: ScreenUtil().L(150),
                ),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  constraints: BoxConstraints(maxWidth: ScreenUtil().L(250)),
                  child: Text(
                    errorString!,
                    style: KFontConstant.myTextStyle(color: Colors.black, size: 17),
                    maxLines: 2,
                  ),
                ),
                Container(
                    margin: EdgeInsets.only(left: ScreenUtil().L(20)),
                    child: InkWell(
                        onTap: () => refreshTap, child: Text("点击刷新", style: KFontConstant.myTextStyle(color: const Color(0xff0059B9), size: 17, bold: true)))),
              ],
            )
          ],
        ),
      ),
    );
  }
}

enum PageState {
  none, //正常
  loading, //加载中
  error, //错误
  empty, //空数据
}
